package com.evil.common.application.enums;

import com.evil.common.core.enums.BaseEnum;
import com.evil.common.family.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationSourceTypeEnum {

    /**
     * 权限表源类型
     */
    PACKAGE(1, "应用包"),
    APPLICATION(2, "应用"),
    GROUP(3, "应用组"),

    PROCESS_NODE_HANDLER(4, "流程节点处理人"),
    PROCESS_NODE_PENDING_DIVERT(5, "流程节点待办转让人"),
    PROCESS_NODE_OVERTIME_NOTIFIER(6, "流程节点超时提醒人"),

    FORM_CONTROL_DEPT_SELECT_SCOPE(7, "表单部门选择范围"),
    FORM_CONTROL_MEMBER_SELECT_SCOPE(8, "表单人员选择范围"),

    REPORTING(9, "报表"),
    MEMBER_CONTROL_DEFAULT_VALUE(10, "成员控件默认值"),
    DEPARTMENT_CONTROL_DEFAULT_VALUE(11, "部门控件默认值"),

    APPLICATION_BUTTON(12, "应用按钮");

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ApplicationSourceTypeEnum> isExistById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationSourceTypeEnum findById(Integer id) {
        return ApplicationSourceTypeEnum.isExistById(id).orElseThrow(ApplicationSourceTypeEnum::getBusinessException);
    }

    public static void forEach(Consumer<ApplicationSourceTypeEnum> consumer) {
        Arrays.asList(values()).forEach(consumer);
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(BaseEnum.ERROR_PARAMETER, "权限源类型参数错误");
    }
}
